
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TrainingsApp.Model;
using TrainingsApp.Helper;
using WisDragon.Framework.Data.DataAccess;

namespace TrainingsApp.DAL {
    public class TS_User_DAL {
        public static TS_User_Collecion TS_User_ListTopByUserType(int userType) {
            TS_User_Collecion list = new TS_User_Collecion();
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_ListTopByUserType");
                cmd.SetParameterValue("@userType", userType);

                List<TS_User_Entity> pageList = cmd.ExecuteEntityList<TS_User_Entity>();
                list.TS_User_List = pageList;

            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return list;

        }

        public static TS_User_Entity TS_User_View(System.Int64 Idx) {
            TS_User_Entity c = default(TS_User_Entity);
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_View");
                cmd.SetParameterValue("@Idx", Idx);
                c = cmd.ExecuteEntity<TS_User_Entity>();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
            return c;
        }

        public static TS_User_Entity TS_User_Add(TS_User_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Add");
                cmd.SetParameterValue("@UserName", entity.UserName);
                cmd.SetParameterValue("@Password", entity.Password);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@BirthDay", entity.BirthDay);
                cmd.SetParameterValue("@IdentityCard", entity.IdentityCard);
                cmd.SetParameterValue("@Mobile", entity.Mobile);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@PhotoImage", entity.PhotoImage);
                cmd.SetParameterValue("@UserType", entity.UserType);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }

            return entity;
        }

        public static void TS_User_Update(TS_User_Entity entity) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Update");
                cmd.SetParameterValue("@Idx", entity.Idx);
                cmd.SetParameterValue("@UserName", entity.UserName);
                cmd.SetParameterValue("@Password", entity.Password);
                cmd.SetParameterValue("@Name", entity.Name);
                cmd.SetParameterValue("@Gender", entity.Gender);
                cmd.SetParameterValue("@BirthDay", entity.BirthDay);
                cmd.SetParameterValue("@IdentityCard", entity.IdentityCard);
                cmd.SetParameterValue("@Mobile", entity.Mobile);
                cmd.SetParameterValue("@Email", entity.Email);
                cmd.SetParameterValue("@PhotoImage", entity.PhotoImage);
                cmd.SetParameterValue("@UserType", entity.UserType);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }

        public static void TS_User_Delete(System.Int64 Idx) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Delete");
                cmd.SetParameterValue("@Idx", Idx);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
            }
        }

        public static bool TS_User_UserNameExists(string userName) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_Exists");
                cmd.SetParameterValue("@Username", userName);
                long len = cmd.ExecuteScalar<long>();
                return len > 0;

            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
                return true;
            }

        }



        public static bool TS_User_ExistsEmailAndUserName(string userName, string email) {
            try {
                DataCommand cmd = DataCommandManager.GetDataCommand("TS_User_ExistsEmailAndUserName");
                cmd.SetParameterValue("@Username", userName);
                cmd.SetParameterValue("@Email", email);
                long len = cmd.ExecuteScalar<long>();
                return len > 0;

            }
            catch (Exception ex) {
                ExceptionHelper.ProcessException(ex);
                return true;
            }

        }
    }
}
